package com.walle.ak47.commons.redis;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisException;


public class RedisSet {

	public static Logger logger = Logger.getLogger(RedisSet.class);
	
	private JedisPool pool;

	private String setName;

	public RedisSet(JedisPool pool, String name) {
		this.pool = pool;
		this.setName = name;
	}
	
    public void release(Jedis jedis, boolean isBroken) {
		if (jedis != null) {
			if (isBroken) {
				pool.returnBrokenResource(jedis);
			} else {
				pool.returnResource(jedis);
			}
		}
	}

	/**
	 * 添加元素 (已测试)
	 *
	 * @param values
	 *            要添加的元素
	 *
	 * @return 实际被添加的元素数量
	 */
	public int add(final String... values) 
	{
		
		int size = 0 ;
    	
    	boolean isBroken = false;
		Jedis jedis = null;
		try
		{
			jedis = pool.getResource() ;
			size = jedis.sadd(setName, values).intValue();
		}catch (JedisException e) {
			isBroken = true;
			logger.warn("failed:add(" + values.toString() + "),count=1" , e);
			throw e;
		} finally {
			release(jedis, isBroken);
		}
		
		return size ;
	}
	
	/**
	 * 删除set中的元素  (已测试)
	 * @param value
	 * @return 
	 */
    public int remove( String value )
    {

    	int size = 0 ;
    	
    	boolean isBroken = false;
		Jedis jedis = null;
		try
		{
			jedis = pool.getResource() ;
			size = jedis.srem(setName,value).intValue();
		}catch (JedisException e) {
			isBroken = true;
			logger.warn("failed:remove(" + value + "),count=1" , e);
			throw e;
		} finally {
			release(jedis, isBroken);
		}
		
		return size ;
    }

	/**
	 * 获取集合大小  (已测试)
	 *
	 * @return 集合大小
	 */
	public int size() 
	{

    	int size = 0 ;
    	
    	boolean isBroken = false;
		Jedis jedis = null;
		try
		{
			jedis = pool.getResource() ;
			size = jedis.scard(setName).intValue();
		}catch (JedisException e) {
			isBroken = true;
			logger.warn("failed:size();" , e);
			throw e;
		} finally {
			release(jedis, isBroken);
		}
		
		return size ;
	}

	/**
	 * 获取集合中的所有值  (已测试)
	 *
	 * @return 集合中的所有值
	 */
	public List<String> getAll() 
	{

		List<String> result ;
    	
    	boolean isBroken = false;
		Jedis jedis = null;
		try
		{
			jedis = pool.getResource() ;
			result = new ArrayList<>(pool.getResource().smembers(setName));
		}catch (JedisException e) {
			isBroken = true;
			logger.warn("failed:size();" , e);
			throw e;
		} finally {
			release(jedis, isBroken);
		}
		
		return result ;
	}

	/**
	 * 判断集合中是否包含指定元素  (已测试)
	 *
	 * @param value
	 *            查询的元素
	 *
	 * @return 是否包含
	 */
	public boolean contains(final String value) 
	{
		
		boolean result = false ;
		
    	boolean isBroken = false;
		Jedis jedis = null;
		try
		{
			jedis = pool.getResource() ;
			result = jedis.sismember(setName, value);
		}catch (JedisException e) {
			isBroken = true;
			logger.warn("failed:size();" , e);
			throw e;
		} finally {
			release(jedis, isBroken);
		}
		
		return result ;
	}
}
